package cn.watson.basic.controller;

import cn.watson.basic.dto.LoginDto;
import cn.watson.basic.exception.BusinessException;
import cn.watson.basic.service.ILoginService;
import cn.watson.basic.utils.AjaxResult;
import cn.watson.user.vo.WxDto;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("login")
public class LoginController {

    @Autowired
    private ILoginService loginService;

    @PostMapping("/account")
    public AjaxResult account(@RequestBody LoginDto loginDto){
        try {
            // 调用业务层实现登录。返回值为token+logininfo用户对象信息
            Map<String, Object> map = loginService.account(loginDto);
            // 返回信息，将token+logininfo放到 setResultObj字段，响应给前端
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setsuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setsuccess(false).setMessage("系统繁忙，请重试！");
        }
    }


    /**
    * @Title: wechat
    * @Description: 微信登录接口
    * @Author: ZhangRui
    * @Version: 1.0
    * @Date:  2022/7/10 18:55  
    * @Parameters: [wxDto]
    * @Return cn.watson.basic.utils.AjaxResult
    */
    @PostMapping("/wechat")
    public AjaxResult wechat(@RequestBody WxDto wxDto){
        try {
            return loginService.wechat(wxDto);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setsuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setsuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

    @PostMapping("/wechat/binder")
    public AjaxResult binder(@RequestBody WxDto wxDto){
        try {

            Map<String, Object> map = loginService.binder(wxDto);
            return AjaxResult.me().setResultObj(map);
        } catch (BusinessException e){
            e.printStackTrace();
            return AjaxResult.me().setsuccess(false).setMessage(e.getMessage());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setsuccess(false).setMessage("系统繁忙，请重试！");
        }
    }

}
